<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" v-if="optType !='view'">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['mes:md:mditem:add']"
        >新增仓库</el-button>
        <WareHouseSelect ref="wareHouseSelect" @onSelected="onWarehouseSelected"
                         :itemId="itemId"></WareHouseSelect>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['mes:md:mditem:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    </el-row>

    <el-table v-loading="loading" :data="warehouseRefList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="仓库" align="center" prop="warehouseName" />
      <el-table-column label="仓库编码" align="center" prop="warehouseCode" />
      <el-table-column label="库区" align="center" prop="locationName" />
      <el-table-column label="库区编码" align="center" prop="locationCode" />
      <el-table-column label="库位" align="center" prop="areaName" />
      <el-table-column label="库位编码" align="center" prop="areaCode" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="optType !='view'">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDeleteSingle(scope.row)"
              v-hasPermi="['mes:md:mditem:remove']"
          >删除</el-button>
        </template>
      </el-table-column>

    </el-table>


    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />



  </div>
</template>

<script>

import {addMdItemWarehouseRef,delMdItemWarehouseRefByIds,getMdItemWarehouseList} from "@/api/mes/md/mdItem";
import ItemSelect from "@/components/itemSelect/index.vue";
import VendorSelect from "@/components/vendorSelect/muitl.vue";
import WareHouseSelect from "@/components/itemSelect/warehouse.vue";
export default {
  name: "Bom",
  components: { ItemSelect ,VendorSelect,WareHouseSelect},
  dicts: ['sys_yes_no', 'mes_item_product','md_phase_code','item_multi_flag','mes_vendor_level','sys_yes_no'],
  props: {
    optType: String,
    itemId: String,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品BOM关系表格数据
      itemRefList: [],
      //供应商关联关系表数据
      vendorRefList:[],
      //物料 仓库关联关系
      warehouseRefList: [],
      // 版本输入框的值
      // bomVersion: '', // 用于存储当前标签页的版本信息
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // itemId: this.itemId,
        mdItemId: this.itemId
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        quantity: [
          { required: true, message: "物料使用比例不能为空", trigger: "blur" }
        ],
        bomVersion: [
          { required: true, message: "bom版本号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    // 监听 bomVersion 和 bomVersionId 的变化，重新加载数据
    // 监听 itemId 的变化，重新加载数据
    itemId(newVal) {
      console.log('itemId newVal-', newVal);
      if (newVal) {
        // this.queryParams.itemId = newVal;
        this.queryParams.mdItemId = newVal;
        this.getList();
      }
    },
    mdItemId(newVal){
      console.log('mdItemId----',mdItemId);
      if (newVal) {
        // this.queryParams.mainItemId = newVal;
        // this.mainItemId=newVal;
        this.queryParams.mdItemId = newVal;
        this.getList();
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {

    /** 查询物料和供应商关系 */
    getList() {
      this.loading = true;
      this.queryParams.mdItemId=this.itemId;
      getMdItemWarehouseList(this.queryParams).then(response => {
        this.warehouseRefList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mdItemId: null,
        areaId: null,
        areaCode: null,
        areaName: null,
        locationId:null,
        locationCode:null,
        locationName:null,
        warehouseId:null,
        warehouseCode:null,
        warehouseName:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.mdItemWarehouseId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.tabName = this.tabName; // 设置表单中的 tabName
      this.$refs.wareHouseSelect.showFlag = true;
    },

    /** 删除按钮操作--批量 */
    handleDelete(rows) {
      console.log('rows--',rows);
      const ids = rows.id || this.ids;
      this.form.mdItemWarehouseIds=ids;
      this.$modal.confirm(`是否确认编号为"${ids}"的数据项？`).then(() => {
        delMdItemWarehouseRefByIds(this.form).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
      }).catch(() => {});
    },
    /** 删除按钮操作--单个 */
    handleDeleteSingle(row) {
      console.log('row--',row);
      const id = row.mdItemWarehouseId;
      // 将单个 ID 构造成数组
      this.form.mdItemWarehouseIds = [id]; // 包装成数组
      this.$modal.confirm(`是否确认删除编号为"${id}"的数据项？`).then(() => {
        delMdItemWarehouseRefByIds(this.form).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
      }).catch(() => {});
    },

    //仓库选择
    onWarehouseSelected(obj) {
      console.log('----onWarehouseSelected obj--',obj);
      if (obj != undefined && obj != null) {
        obj.forEach(element => {
          this.form.mdItemId=this.itemId;
          this.form.areaId=element.areaId;
          this.form.areaCode=element.areaCode;
          this.form.areaName=element.areaName;
          this.form.locationId=element.locationId;
          this.form.locationCode=element.locationCode;
          this.form.locationName=element.locationName;
          this.form.warehouseId=element.warehouseId;
          this.form.warehouseCode=element.warehouseCode;
          this.form.warehouseName=element.warehouseName;
          addMdItemWarehouseRef(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.getList();
          });
        });
      }
    }
  }
};
</script>
